package upload;
import java.io.BufferedInputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

import ui.FileUploadUI;
import util.HttpUtil;

public class SendToHost {
	private String path = null;
	private String host = null;
	private Socket socket = null;
	private byte[] content = null;
	private int contentLen = 0;

	public SendToHost(String host, String path) {
		this.path = path;
		this.host = host;
		content = new byte[0];
		contentLen = 0;
	}
	
	public String send(){
		StringBuffer sb = new StringBuffer();
		OutputStream os = null;
		BufferedInputStream is = null;
		try{
			socket = new Socket(FileUploadUI.host, 80);
			socket.setSoTimeout(10000);
			os = socket.getOutputStream();
			is = new BufferedInputStream(socket.getInputStream());
			os.write(("GET " + path + " HTTP/1.1\r\n").getBytes());
			os.write(("Host: " + host + "\r\n").getBytes());
			os.write(("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; zh-CN; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1\r\n").getBytes());
			os.write(("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n").getBytes());
			os.write(("Accept-Language: zh-cn,zh;q=0.5\r\n").getBytes());
			os.write(("Accept-Encoding: gzip,deflate\r\n").getBytes());
			os.write(("Accept-Charset: gb2312,utf-8;q=0.7,*;q=0.7\r\n").getBytes());
			os.write(("Connection: close\r\n").getBytes());
			os.write(("Cache-Control: max-age=0\r\n").getBytes());
			if(contentLen!=0)
				os.write(("Content-Length: "+content.length+"\r\n").getBytes());
			os.write(("\r\n").getBytes());
			if(contentLen!=0)
				os.write(content);
			os.flush();

			String temp = null;
			int contentLength = -1;
			while (true)
			{
				temp = HttpUtil.readLine(is);
				if (temp == null || temp.length() == 0)
					break;
				if (HttpUtil.startsWith(temp, "Content-Length: "))
				{
					try
					{
						contentLength = Integer.parseInt(temp.substring(16));
					}
					catch (NumberFormatException e)
					{
						contentLength = -2;
					}
				}
			}
			byte[] buf = null;
			int bytes_read;
			
			if (contentLength >= 0)
			{
				buf = new byte[contentLength + 10];
	
				int tempBuf = 0;
				while ((bytes_read = is.read(buf)) != -1)
				{
					sb.append(new String(buf, 0, bytes_read));
					tempBuf += bytes_read;
					if (tempBuf >= contentLength)
					{
						break;
					}
				}
			}
			else
			{
				buf = new byte[96000];
				while ((bytes_read = is.read(buf)) != -1)
				{
					if(!HttpUtil.startsWith(new String(buf, 0, bytes_read), "X-Trickling: "))
					{
						sb.append(new String(buf, 0, bytes_read));
					}
				}
			}
		}catch(Exception e){
			try {
				os.close();
			} catch (Exception e1) {
			}
			try {
				is.close();
			} catch (Exception e1) {
			}
			try {
				socket.close();
			} catch (Exception e1) {
			}
		}
		return sb.toString();
	}

	public byte[] getContent() {
		return content;
	}

	public void setContent(String content) {
		try {
			this.content = content.getBytes("UTF-8");
			contentLen = this.content.length;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public void setContent(byte[] b,int len) {
		this.content = b;
		contentLen = len;
	}
}
